package game;

import game.Audio.AudioResources;
import game.Gamemode.StandardManager;
import game.Gamemode.TeamDeathmatchManager;
import game.Map.Map;
import game.Player.BotController;
import javafx.scene.control.CheckBox;
import javafx.scene.control.ComboBox;
import javafx.scene.control.ListView;
import javafx.scene.control.TextField;


import java.io.IOException;
import java.util.HashMap;

class StartGame {

    /***
     * Gets value from TextField as Integer or a default value if any error occurs.
     *
     * @param tf                TextField to extract the Integer.
     * @param defaultValue      Default value to be returned in case an exception is fired.
     */
    private Integer getIntegerFromText(TextField tf, Integer defaultValue) {
        try {
            return Integer.parseInt(tf.getText());
        } catch (NumberFormatException e) {
            return defaultValue;
        }
    }

    /***
     * Gets information required to instantiate a GameModeManager and starts the new game.
     */
    public StartGame() {
        // Cargar mapa
        try {
            HashMap<String, TextField> textFields = StartMenu.getTextFields();
            Integer winLimit = getIntegerFromText(textFields.get("Win limit (Rounds):"), 15);
            Integer roundLimit = getIntegerFromText(textFields.get("Team Round Limit (Rounds):"), 15);
            Integer timePerRound = getIntegerFromText(textFields.get("Time per Round (Sec.):"), 120);
            Integer freezeTime = getIntegerFromText(textFields.get("Freeze Time (Sec.):"), 5);
            Integer buyTime = getIntegerFromText(textFields.get("Buy Time (Sec.):"), 10);
            Integer startMoney = getIntegerFromText(textFields.get("Start Money:"), 800);
            String botNamePrefix = textFields.get("Bot Name Prefix:").getText();
            Integer nBots = getIntegerFromText(textFields.get("Bots:"), 9);

            HashMap<String, CheckBox> checkBoxes = StartMenu.getCheckBoxes();
            boolean friendlyFire = checkBoxes.get("Friendly Fire").isSelected();
            boolean counterTeam = checkBoxes.get("Counter Terrorist Team").isSelected();
            //TODO: Revisar ubicacion
            boolean fogOfWar = checkBoxes.get("Fog of War").isSelected();
            Configuration.setTacticVision(fogOfWar);
            //boolean autoTeamBalance = checkBoxes.get("Auto Teambalance").isSelected();
            //boolean autoFill = checkBoxes.get("Auto Fill").isSelected();

            HashMap<String, ComboBox> comboBoxes = StartMenu.getComboBoxes();
            String skill = comboBoxes.get("Skills:").getSelectionModel().getSelectedItem().toString();
            BotController.Difficulty botLevel = BotController.difficultyFromString(skill);

            ListView<String> mapList = StartMenu.getMapList();
            String mapName = mapList.getSelectionModel().getSelectedItem();
            StartMenu.hideMenu();
            AudioResources.init();
            Map map = new Map(new Vector3(0.0, 0.0, 0.0), 1.0, mapName);
            Scene s = new Scene(map);
            //If player name is empty it defaults to "Player" in both game and configuration
            //TODO: ¿se debe cambiar el fichero de configuracion o no?
            if (Configuration.getNamePlayer().equals("")) {
                Configuration.setNamePlayer("Player");
                Configuration.getInstance().changeConfiguration("Configuration", "Name-player", "Player");
            }
            if(comboBoxes.get("Game Mode:").getSelectionModel().getSelectedIndex() == 0) {
                TeamDeathmatchManager tdm = new TeamDeathmatchManager(freezeTime, buyTime, timePerRound, 10,
                        roundLimit, winLimit, friendlyFire, nBots, botNamePrefix, startMoney, botLevel, counterTeam);
            }
            else {
                StandardManager stm = new StandardManager(freezeTime, buyTime, timePerRound, 10,
                        roundLimit, winLimit, friendlyFire, nBots, botNamePrefix, startMoney, botLevel, counterTeam);
            }
            s.getCamera().initStore();
        } catch (IOException e) {
            e.printStackTrace();
            System.exit(1);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
